package com.fneuch.mwg;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fneuch.mwg.army.ArmyList;
import com.fneuch.mwg.army.Unit;
import com.fneuch.mwg.rules.AttackResult;
import com.fneuch.mwg.rules.CloseCombatRuleEngine;
import com.fneuch.mwg.rules.ShootingRuleEngine;


@Component
@Scope("session")
public class GameSystem {

	@Autowired
	ShootingRuleEngine shootingEngine;
	
	@Autowired
	CloseCombatRuleEngine closeCombatEngine;
	
	private ArmyList attackers = new ArmyList();
	private ArmyList defenders = new ArmyList();

	
	public GameSystem() {
	}
	
	public List<Unit> getAttackers() {
		return attackers;
	}

	public List<Unit> getDefenders() {
		return defenders;
	}

	public AttackResult executeShooting() {
		AttackResult result = null; 
		if(!attackers.isEmpty() && !defenders.isEmpty()) {
			result = executeShooting(attackers.get(0), defenders.get(0));
		}
		return result;
	}

	public AttackResult executeShooting(Unit attacker, Unit defender) {
		AttackResult result = null; 
		if(attacker != null && defender != null && !attacker.getModels().isEmpty() && !defender.getModels().isEmpty()) {
			result = shootingEngine.execute(attacker, defender);
		}
		return result;
	}

	public AttackResult executeCloseCombat() {
		AttackResult result = null; 
		if(!attackers.isEmpty() && !defenders.isEmpty()) {
			result = executeCloseCombat(attackers.get(0), defenders.get(0));
		}
		return result;
	}

	private AttackResult executeCloseCombat(Unit attacker, Unit defender) {
		AttackResult result = null; 
		if(attacker != null && defender != null && !attacker.getModels().isEmpty() && !defender.getModels().isEmpty()) {
			result = closeCombatEngine.execute(attacker, defender);
		}
		return result;
	}
	
}
